/**
 * @Description: TODO
 * @return TODO
 * @author liveneo-shuqw
 * @date 2016年6月13日 下午4:26:19 Copyright: Copyright (c) 2015 Company:北京朗泰
 * @version 3.0
 */
package com.liveneo.adapter.mobile.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.liveneo.adapter.base.entity.PrpmImageSurvey;
import com.liveneo.adapter.base.entity.PrpmImageSurveyRemark;
import com.liveneo.adapter.base.entity.PrpmSurveyUser;
import com.liveneo.adapter.base.entity.PrpmUploadImage;
import com.liveneo.adapter.base.mapper.PrpmImageSurveyMapper;
import com.liveneo.adapter.base.mapper.PrpmImageSurveyRemarkMapper;
import com.liveneo.adapter.base.mapper.PrpmSurveyUserMapper;
import com.liveneo.adapter.base.mapper.PrpmUploadImageMapper;
import com.liveneo.adapter.base.support.BaseServiceImpl;
import com.liveneo.adapter.mobile.model.dto.MobileImageRemarkDto;
import com.liveneo.adapter.mobile.model.request.LossConfirmRequest;
import com.liveneo.adapter.mobile.model.request.UploadImageRemarkRequest;
import com.liveneo.adapter.mobile.service.MobileImageService;
import com.liveneo.adapter.mobile.util.UUIDUtils;

/**
 * @ClassName: MobileImageServiceImpl
 * @Description: TODO
 * @author liveneo-shuqw
 * @date 2016年6月13日 下午4:26:19
 * @return
 */
@Service
public class MobileImageServiceImpl extends BaseServiceImpl implements MobileImageService {
    @Autowired
    private PrpmImageSurveyMapper prpmImageSurveyMapper;
    @Autowired
    private PrpmImageSurveyRemarkMapper prpmImageSurveyRemarkMapper;
    @Autowired
    PrpmUploadImageMapper prpmUploadImageMapper;
    @Autowired
    PrpmSurveyUserMapper prpmSurveyUserMapper;

    @Override
    public int saveUploadImageRemark(UploadImageRemarkRequest request) throws Exception {
        // 查询图片查勘信息
        List<PrpmImageSurvey> images = prpmImageSurveyMapper.selectByRequest(request);
        // 查询查勘员相关信息
        PrpmSurveyUser prpmSurveyUser = prpmSurveyUserMapper.findByUserCode(request.getUserId());
        if(prpmSurveyUser == null){
            throw new Exception("查勘员代码在查勘平台不存在，请维护查勘员！");
        }
        // 保存图片查勘备注信息
        PrpmImageSurveyRemark pr = new PrpmImageSurveyRemark();
        pr.setImageSurveyRemarkId(UUIDUtils.getUUIDStr());
        if (images != null && images.size() > 0) {
            PrpmImageSurvey ps = images.get(0);
            pr.setImageSurveyId(ps.getImageSurveyId());
        }
        pr.setCaseInfoId(request.getCaseUuid());
        pr.setLossItemId(request.getLossUuid());
        pr.setCaseNo(request.getCaseNo());
        pr.setUserUuid(prpmSurveyUser.getSurveyUserId());
        pr.setUserName(prpmSurveyUser.getSurveyName());
        pr.setUserCode(request.getUserId());
        pr.setContent(request.getContent());
        pr.setMsgType("1");//1.双向留言；2.拍照查勘留言
        pr.setPlatType("2");//1.平台；2.手机
        pr.setCreateBy(prpmSurveyUser.getSurveyName());
        pr.setCreateTime(new Date());
        return prpmImageSurveyRemarkMapper.insert(pr);
    }

    @Override
    public List<MobileImageRemarkDto> getRemarkByLossUuid(String lossuuid) {
        List<PrpmImageSurveyRemark> remarkList = prpmImageSurveyMapper.getRemarkByLossUuid(lossuuid);
        List<MobileImageRemarkDto> remarks = new ArrayList<MobileImageRemarkDto>();
        for (PrpmImageSurveyRemark pr : remarkList) {
            MobileImageRemarkDto pd = new MobileImageRemarkDto();
            pd.setCommentId(pr.getImageSurveyRemarkId());
            pd.setCaseNo(pr.getCaseNo());
            pd.setContent(pr.getContent());
//            pd.setCreatePlatType(pr.getFlag());
            pd.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(pr.getCreateTime()));
            pd.setUserName(pr.getUserName());
            remarks.add(pd);
        }
        return remarks;
    }

    @Override
    public List<MobileImageRemarkDto> getRemarkByRequest(LossConfirmRequest request) {
        List<PrpmImageSurveyRemark> remarkList = prpmImageSurveyRemarkMapper.getRemarkByRequest(request);
        List<MobileImageRemarkDto> remarks = new ArrayList<MobileImageRemarkDto>();
        for (PrpmImageSurveyRemark pr : remarkList) {
            MobileImageRemarkDto pd = new MobileImageRemarkDto();
            pd.setCommentId(pr.getImageSurveyRemarkId());
            pd.setCaseNo(pr.getCaseNo());
            pd.setContent(pr.getContent());
            // TODO 
            pd.setCreatePlatType(pr.getPlatType());
            pd.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(pr.getCreateTime()));
            pd.setUserName(pr.getUserName());
            pd.setUserCode(pr.getUserCode());
            remarks.add(pd);
        }
        return remarks;
    }

    @Override
    public void saveSurveyImage(PrpmImageSurvey imageSurvey) {
        prpmImageSurveyMapper.insert(imageSurvey);
    }

    @Override
    public int saveBatchUploadImage(List<PrpmUploadImage> list) {
        return prpmUploadImageMapper.insertBatch(list);
    }

	@Override
	public PrpmImageSurvey getImageSurveyByLossUuid(String lossUuid) {
		
		return prpmImageSurveyMapper.getImageSurveyByLossUuid(lossUuid);
	}

	@Override
	public void updateImageSurveyByLossUuid(PrpmImageSurvey prpmImageSurvey) {
		
		prpmImageSurveyMapper.updateImageSurveyByLossUuid(prpmImageSurvey);
		
	}
}
